//
//  PUPicture.m
//  PicUploader
//
//  Created by Chen Zhang on 8/13/08.
//  Copyright 2008 overboming. All rights reserved.
//

#import "PUPicture.h"


@implementation PUPicture

- (id) init
{
    [super init];
    filePath = nil;
    fileSize = -1;
    picStatus = nil;
    picComment = nil;
    fileName = nil;
    uploadString = nil;
    return self;
}


- (id) initWithPath: (NSString *) path 
               Size: (int) size 
             Status: (NSMutableAttributedString *) status
            Comment:(NSString *) comment
{
    [super init];
    filePath = path; //actually this becomes the file name 
    fileSize = size;
    picStatus = status;
    picComment = comment;
    fileName = [[self getFileNameFromPath:path] retain];
    uploadString = @"";
    //NSLog(@"%@ %d %@ %@",path,size,status,comment);
    return self;
}

- (void)encodeWithCoder:(NSCoder *)coder
{
    [coder encodeObject:filePath forKey:@"filePath"];
    [coder encodeInt:fileSize forKey:@"fileSize"];
    [coder encodeObject:picStatus forKey:@"picStatus"];
    [coder encodeObject:picComment forKey:@"picComment"];
    [coder encodeObject:fileName forKey:@"fileName"];
    
}

- (void)setNilValueForKey:(NSString *)key
{
	if ([key isEqual:@"fileSize"]){
        [self setFileSize:-1];
	}
	else{
		[super setNilValueForKey:key];
	}
}


- (id)initWithCoder:(NSCoder *)coder
{
	[super init];
	filePath = [[coder decodeObjectForKey:@"filePath"] retain];
	fileSize = [coder decodeIntForKey:@"fileSize"];
    picStatus = [[coder decodeObjectForKey:@"picStatus"] retain];
	picComment = [[coder decodeObjectForKey:@"picComment"] retain];
    fileName = [[coder decodeObjectForKey:@"fileName"] retain];
    
	return self;
}


- (void)dealloc
{
    [filePath release];
    [picStatus release];
    [picComment release];
    [fileName release];
    [super dealloc];
}


- (NSString *)getFileNameFromPath: (NSString *) path
{
    NSRange seperatorRange;
    seperatorRange = [path rangeOfString:@"/"
                                 options:(NSBackwardsSearch)];
    NSRange filenameRange;
    filenameRange.location = seperatorRange.location + 1;
    filenameRange.length = [path length] - seperatorRange.location - 1;
    NSString *filename = [path substringWithRange:filenameRange];
    //NSLog(filename);
    return filename;
}


@synthesize filePath;
@synthesize fileSize;
@synthesize picStatus;
@synthesize picComment;
@synthesize fileName;
@synthesize uploadString;
@end
